package com.atguigu.gmall.cart.service.impl;

import com.atguigu.gmall.cart.mapper.CartInfoMapper;
import com.atguigu.gmall.cart.service.CartInfoSyncService;
import com.atguigu.gmall.model.cart.CartInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * @author 李旭
 * @date 2022/1/18 9:28
 * @Description:
 */
@Service
public class CartInfoSyncServiceImpl implements CartInfoSyncService {

    @Autowired
    private CartInfoMapper cartInfoMapper;

    /**
     * 使用临时用户的ID 与 skuId ==唯一ID   更新真实用户的ID
     * @param userId
     * @param userTempId
     * @param skuId
     */
    @Async //异步注解
    @Override
    public void updateSync(String userId, String userTempId, Long skuId) {
        System.out.println("updateSync当前加入购物车的子线程：" + Thread.currentThread().getName());
        //条件 ：  用户ID +  库存ID  == 唯一ID
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
                .eq("user_id",userTempId).
                        eq("sku_id",skuId));
        // update cart_info set user_id = 3
        //      where user_id = 40011d71342d427582a3d9b75c96f3fa and sku_id = ?

    }
    ////异步更新DB
    @Override
    @Async //异步注解
    public void updateSync(CartInfo cartInfo) {
        System.out.println("updateSync当前加入购物车的子线程：" + Thread.currentThread().getName());
        //条件 ：  用户ID +  库存ID  == 唯一ID
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
          .eq("user_id",cartInfo.getUserId()).eq("sku_id",cartInfo.getSkuId()));

    }
    //异步添加DB
    @Override
    @Async //异步注解
    public void insertSync(CartInfo cartInfo) {
        System.out.println("insertSync当前加入购物车的子线程：" + Thread.currentThread().getName());
        cartInfoMapper.insert(cartInfo);

    }

    //批量 更新用户ID
    @Override
    @Async //异步注解
    public void updateSyncBatch(String userId, String userTempId) {
        System.out.println("updateSyncBatch当前加入购物车的子线程：" + Thread.currentThread().getName());
        CartInfo cartInfo = new CartInfo();
        cartInfo.setUserId(userId);
        cartInfoMapper.update(cartInfo,new QueryWrapper<CartInfo>()
           .eq("user_id",userTempId));
        // update cart_info set user_id = 3 where user_id = 40011d71342d427582a3d9b75c96f3fa
    }

    /**
     * 删除
     * @param cartInfoByUserTempId
     */
    @Override
    @Async //异步注解
    public void deleteSync(CartInfo cartInfoByUserTempId) {
        System.out.println("deleteSync当前加入购物车的子线程：" + Thread.currentThread().getName());
        cartInfoMapper.delete(new QueryWrapper<CartInfo>()
          .eq("user_id",cartInfoByUserTempId.getUserId())
          .eq("sku_id",cartInfoByUserTempId.getSkuId()));
    }


}
